<?php
function no_index() {
	return "<meta name=\"robots\" content=\"noindex,nofollow\" />";
}

function view_404() {
	header('HTTP/1.0 404 Not Found');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<?php echo no_index()."\n"; ?>
		<style type="text/css">
			body {
				background-color: #fff;
				margin: 40px;
				font-family: Lucida Grande, Verdana, Sans-serif;
				font-size: 12px;
				color: #000;
			}
			#content {
				border: #999 1px solid;
				background-color: #fff;
				padding: 20px 20px 12px 20px;
			}
			h1 {
				font-weight: normal;
				font-size: 14px;
				color: #990000;
				margin: 0 0 4px 0;
			}
		</style>
		<title>404 Page Not Found</title>
	</head>
	<body>
		<div id="content">
			<h1>404 Page Not Found</h1>
			<p>The page you requested was not found.</p>
		</div>
	</body>
</html>
<?php
}

function view_header_top() {
}

function view_header_bottom() {
}

function view_footer_top() {
}

function view_footer_bottom() {
}

function int_to_rp($int) {
	$int = str_split((string)$int);
	$i = 0;
	$retval = "";
	for ($j = count($int) - 1; $j >= 0; $j--) {
		if ($i == 3) {
			$retval = $int[$j].".".$retval;
			$i = 1;
		} else {
			$retval = $int[$j].$retval;
			$i++;
		}		
	}
	$retval = "Rp ".$retval;
	return $retval;
}

function rp_to_int($rp) {
	$rp = explode(" ",$rp);
	$rp = $rp[1];
	$rp = explode(".",$rp);
	$retval = "";
	foreach ($rp as $tiga) {
		$retval .= $tiga;
	}
	return intval($retval);
}

function amankan($str) {
	// sebelum suatu string dimasukkan ke query mysql,
	// amankan dulu pake fungsi ini.
	// yang disarankan pake yg versi 1 itu pake bawaan mysql
	// tapi di beberapa hosting ga bisa jadi pake yg versi 2
	
	/* versi 1 (disarankan) */
	$retval = mysql_real_escape_string($str);
	/* end of versi 1 (disarankan) */
	
	/* versi 2 (tested) */
	// $retval = str_replace("\\","\\\\",$str);
	// $retval = str_replace('"','\\"',$retval);
	// $retval = str_replace("'","\\'",$retval);
	// $retval = str_replace("\r","\\r",$retval);
	// $retval = str_replace("\n","\\n",$retval);
	// $retval = str_replace("\x00","\\0",$retval);
	// $retval = str_replace("\x1a","\\Z",$retval);
	/* end of versi 2 (tested) */
	
	return $retval;
}

function select_status($index,$status) {
	if ($index == $status) return " selected=\"selected\"";
	else return "";
}

function checkbox_status($index,$status) {
	if ($index == $status) return " checked=\"checked\"";
	else return "";
}

function slugging($string,$table,$id) {
	$string = preg_replace("/[^a-zA-Z0-9]/","-",strtolower($string));
	while (true) {
		$thing = db_result("SELECT * FROM $table WHERE slug = '$string' AND id <> $id");
		if ($thing == false) {
			$retval = $string;
			break;
		} else {
			$string .= "-1";
		}
	}
	return $retval;
}

function kategoring($kategori) {
	$temp = explode(',',$kategori);
	foreach ($temp as $key => $piece) {
		$piece = strtolower(trim($piece));
		if ($piece == '') {
			unset($temp[$key]);
		} else {
			$temp[$key] = $piece;
		}
	}
	$temp = array_unique($temp);
	$retval = "";
	foreach ($temp as $temporary) {
		$retval .= $temporary.",";
	}
	$retval = substr($retval,0,strlen($retval)-1);
	return $retval;
}

function rapikan_kategori() {
	$kategoris = array();
	$produks = db_array("SELECT * FROM produk");
	foreach ($produks as $produk) {
		$kategori_kategori = $produk['kategori'];
		$kategori_kategori = explode(',',$kategori_kategori);
		foreach ($kategori_kategori as $kategori) {
			$temp3 = db_result("SELECT * FROM kategori WHERE isi = '$kategori'");
			if ($temp3 == false) {
				$id = db_insert("INSERT INTO kategori (isi) VALUES ('$kategori')");
				$slug = slugging($kategori,"kategori",$id);
				db_query("UPDATE kategori SET slug = '$slug' WHERE id = $id");
			}
			$kategoris[] = $kategori;
		}
	}
	$semua_kategori = db_array("SELECT * FROM kategori");
	foreach ($semua_kategori as $kat) {
		if (!in_array($kat['isi'],$kategoris)) {
			db_query("DELETE FROM kategori WHERE id = ".$kat['id']);
		}
	}
}

function random_string($l = 10){
    $c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxwz";
    $s = "";
	for(;$l > 0;$l--) $s .= $c{rand(0,strlen($c))};
    return str_shuffle($s);
}
?>